﻿
package com.ostrich.project.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.service.BaseService;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.bean.TostHrDepartinfo;
import com.ostrich.manpower.bean.TostHrPostinfo;
import com.ostrich.manpower.service.impl.TostHrBaseinfoService;
import com.ostrich.manpower.service.impl.TostHrDepartinfoService;
import com.ostrich.manpower.service.impl.TostHrPostinfoService;
import com.ostrich.project.bean.TostPjCostitem;
import com.ostrich.project.bean.TostPjEngdepartcost;
import com.ostrich.project.service.TostPjEngdepartcostDao;
/**
 * @author wangyinan
 * @brief    模块功能简单描述
 * @details  模块功能详细描述
 */
@Service("tostPjEngdepartcostService")
public class TostPjEngdepartcostService<T> extends BaseService<T> {
	private final static Logger log= Logger.getLogger(TostPjEngdepartcostService.class);

	@Autowired
    private TostPjEngdepartcostDao<T> dao;
	@Autowired(required=false)
	private TostHrBaseinfoService<TostHrBaseinfo> tosthrbaseinfoservice;
	@Autowired(required=false)
	private TostHrDepartinfoService<TostHrDepartinfo> tosthrdepartinfoservice;
	@Autowired(required=false)
	private TostHrPostinfoService<TostHrPostinfo> tosthrpostinfoservice;
	
	
	public TostPjEngdepartcostDao<T> getDao() {
		return dao;
	}
	
	public void addOrUpdateEng(TostPjEngdepartcost tped,String nickname) throws Exception{
		
		int hrid=tped.getHrId();
		float lak=tped.getPenLakeproject();//项目系数
		int ser=tped.getPenSertime();//服务时间
		float month=tped.getPenMonthcost();//月人工成本
		
		float allcost=lak*ser*month;//人工总成本=项目系数x服务时间x月人工成本。
		tped.setPenAllcost(allcost);
		
		TostHrBaseinfo thb=tosthrbaseinfoservice.queryById(hrid);
		String hdpid = null;
		//for(TostHrBaseinfo thb:list){
			hdpid=thb.getHrDepart();
			tped.setPenName(thb.getHrName());
			tped.setHdpId(hdpid);//部门id
		//}
		
		int hpoid=tped.getHpoId();
		TostHrPostinfo thp=tosthrpostinfoservice.queryById(hpoid);
		tped.setPenJobs(thp.getHpoName());
		
		TostHrDepartinfo thd=tosthrdepartinfoservice.queryById(hdpid);//部门信息
		if(thd==null){
			thd=tosthrdepartinfoservice.queryById(1);
			tped.setHdpId(thd.getId());
		}
		tped.setHdpName(thd.getHdpName());//部门名字
		
		String createtime=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		
		tped.setCreatetime(createtime);
		tped.setPerson(nickname);
		
		if(tped.getId()==null)
			this.add((T) tped);
		else
			this.update((T) tped);
	}
	
	public void deleteByPctId(Integer pctId) throws Exception{
		 dao.deleteByPctId(pctId);
	}
}
